package com.fzrskj.cloud.free.user.service;

import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.user.utils.UserPersonalUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * UserPersonalService.
 *
 * @author zj.
 *         Created on 2019/2/25 0025.
 */
public class UserPersonalService extends CObject {
    /**
     * 用户信息修改验证码获取
     */
    public String userPersonalOldCheckCode(HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        int uid = CloudContextUtils.getSession().getUID();

        String way = JsonTool.getStrValue(p,"way", "");
        String bizCode = JsonTool.getStrValue(p, "bizCode", "");

        if (StrTool.isBlankStr(way) || StrTool.isBlankStr(bizCode)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        UserPersonalUtils personalUtils = new UserPersonalUtils();
        Boolean result = personalUtils.sendOldCheckCode(uid, way, bizCode);
        if (!result) {
            return XmlCodeParser.formatResultTips(false, personalUtils.getErrCode(), personalUtils.getMsg());
        }

        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 用户手机号修改：获取新手机号验证码.
     */
    public String userPersonalMobileCheckCode(HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        int uid = CloudContextUtils.getSession().getUID();

        String mobile = JsonTool.getStrValue(p,"mobile", "");

        if (StrTool.isBlankStr(mobile)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        UserPersonalUtils personalUtils = new UserPersonalUtils();
        Boolean result = personalUtils.sendMobileCheckCode(uid, mobile);
        if (!result) {
            return XmlCodeParser.formatResultTips(false, personalUtils.getErrCode(), personalUtils.getMsg());
        }

        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 用户手机号修改：修改手机号.
     */
    public String userPersonalMobileEdit (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        int uid = CloudContextUtils.getSession().getUID();

        String oldCheckCode = JsonTool.getStrValue(p, "oldCheckCode", "");
        String newCheckCode = JsonTool.getStrValue(p, "newCheckCode", "");
        String mobile = JsonTool.getStrValue(p,"mobile", "");

        if (StrTool.isBlankStr(mobile) || StrTool.isBlankStr(oldCheckCode) || StrTool.isBlankStr(newCheckCode)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        UserPersonalUtils personalUtils = new UserPersonalUtils();
        Boolean result = personalUtils.modifyUserMobile(uid, mobile, oldCheckCode, newCheckCode);
        if (!result) {
            return XmlCodeParser.formatResultTips(false, personalUtils.getErrCode(), personalUtils.getMsg());
        }

        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 用户邮箱修改：获取新邮箱验证码.
     */
    public String userPersonalMailCheckCode (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        int uid = CloudContextUtils.getSession().getUID();

        String mail = JsonTool.getStrValue(p,"mail", "");

        if (StrTool.isBlankStr(mail)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        UserPersonalUtils personalUtils = new UserPersonalUtils();
        Boolean result = personalUtils.sendMailCheckCode(uid, mail);
        if (!result) {
            return XmlCodeParser.formatResultTips(false, personalUtils.getErrCode(), personalUtils.getMsg());
        }

        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 用户邮箱修改：修改邮箱.
     */
    public String userPersonalMailEdit (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        int uid = CloudContextUtils.getSession().getUID();

        String oldCheckCode = JsonTool.getStrValue(p, "oldCheckCode", "");
        String newCheckCode = JsonTool.getStrValue(p, "newCheckCode", "");
        String mail = JsonTool.getStrValue(p,"mail", "");

        if (StrTool.isBlankStr(mail) || StrTool.isBlankStr(oldCheckCode) || StrTool.isBlankStr(newCheckCode)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        UserPersonalUtils personalUtils = new UserPersonalUtils();
        Boolean result = personalUtils.modifyUserMail(uid, mail, oldCheckCode, newCheckCode);
        if (!result) {
            return XmlCodeParser.formatResultTips(false, personalUtils.getErrCode(), personalUtils.getMsg());
        }

        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }
}
